package com.jk.vehicle.mini.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jk.vehicle.mini.service.ElectricBicycleService;
import com.jk.vehicle.mini.dao.ElectricBicycleMapper;
import com.jk.vehicle.mini.entity.ElectricBicycle;
import com.jk.vehicle.core.service.impl.AbstractService;
import com.jk.vehicle.mini.util.QueryUtil;
import com.jk.vehicle.mini.util.SequenceUtils;
import com.jk.vehicle.mini.vo.BicycleDetailOfUserVO;
import com.jk.vehicle.mini.vo.BicycleLinkIdVO;
import com.jk.vehicle.mini.vo.BicycleOfUserVO;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * This is Description for ElectricBicycleServiceImpl
 *
 * @author Administrator
 * @date 2021/02/07
 */
@Service
public class ElectricBicycleServiceImpl extends AbstractService<ElectricBicycle> implements ElectricBicycleService {
    /**
	 * 注入ElectricBicycleMapper
	 */
    @Resource
    private ElectricBicycleMapper electricBicycleMapper;

    /**
    * 根据查询条件查询单表数据
    * @param paramMap 查询条件
    * @return 查询结果
    */
    public List<ElectricBicycle> queryByParams(Map<String, Object> paramMap){
        Condition condition = new Condition(ElectricBicycle.class);
        Example.Criteria criteria = condition.createCriteria();
        Map<String, EntityColumn> propertyMap = EntityHelper.getEntityTable(ElectricBicycle.class).getPropertyMap();
        QueryUtil.spliceQueryParam(criteria, paramMap,  propertyMap, condition);
        return electricBicycleMapper.selectByCondition(condition);
    }

    @Override
    @DS("#last")
    public void save(ElectricBicycle electricBicycle, String dsName) {
        electricBicycle.setId(SequenceUtils.nextId());
        electricBicycle.setCreateDate(new Date());
        super.save(electricBicycle, dsName);
    }

    @Override
    @DS("#last")
    public void update(ElectricBicycle electricBicycle, String dsName){
        electricBicycle.setUpdateDate(new Date());
        super.update(electricBicycle, dsName);
    }

    /**
     * 根据车辆id查询对应的型号id、厂家id
     * @param bicycleId 车辆id
     * @return 结果
     */
    @Override
    @DS("#last")
    public List<BicycleLinkIdVO> queryBicycleLinkId(Long bicycleId, String dsName){
        return electricBicycleMapper.queryBicycleLinkId(bicycleId);
    }

    /**
     * 查询用户关联的车辆信息
     * @param userId 用户ID
     * @return 结果
     */
    @DS("#last")
    public List<BicycleOfUserVO> queryBicycleOfUser(Long userId, String dsName){
        return electricBicycleMapper.queryBicycleOfUser(userId);
    }

    /**
     * 查询用户关联的车辆信息
     * @param userId 用户ID
     * @return 结果
     */
    @DS("#last")
    public List<BicycleDetailOfUserVO> queryBicycleDetailOfUser(Long userId, Long corpId, String dsName){
        return electricBicycleMapper.queryBicycleDetailOfUser(userId, corpId);
    }
}
